package com.x.erp.manage;

import com.x.erp.dao.IntelligentRobotDao;
import com.x.erp.model.IntelligentRobot;
import com.x.erp.model.SelectTemplate;
import com.x.framework.manage.BaseManage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IntelligentRobotManage extends BaseManage {

	@Autowired
	private IntelligentRobotDao intelligentRobotDao;

	public IntelligentRobot insertIntelligentRobot(IntelligentRobot intelligentRobot) throws Exception {
		if(intelligentRobot.getRobotType() == 2){
			SelectTemplate selectTemplate = intelligentRobot.getSelectTemplate();
//			if(StringUtils.isNotEmpty(selectTemplate.getTemplateId())){
//				this.update(selectTemplate);
//			} else {
				this.insert(selectTemplate);
				intelligentRobot.setTemplateId(selectTemplate.getTemplateId());
//			}
		}
		return this.insert(intelligentRobot);
	}

	public int updateIntelligentRobot(IntelligentRobot intelligentRobot) throws Exception {
		if(intelligentRobot.getRobotType() != null && intelligentRobot.getRobotType() == 2){
			SelectTemplate selectTemplate = intelligentRobot.getSelectTemplate();
			if(StringUtils.isNotEmpty(selectTemplate.getTemplateId())){
				this.update(selectTemplate);
			} else {
				this.insert(selectTemplate);
				intelligentRobot.setTemplateId(selectTemplate.getTemplateId());
			}
		}
		return this.update(intelligentRobot);
	}

	public int deleteIntelligentRobot(IntelligentRobot intelligentRobot) throws Exception {
		IntelligentRobot robot = intelligentRobotDao.selectIntelligentRobot(intelligentRobot);
		if(robot == null){
			return 0;
		}
		if(robot.getRobotType() == 2) {
			SelectTemplate selectTemplate = new SelectTemplate();
			selectTemplate.setTemplateId(robot.getTemplateId());
			this.delete(selectTemplate);
		}
		return this.delete(intelligentRobot);
	}
}
